package com.yuexunit.finance.member.action.v1;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.context.FinanceSessionContext;
import com.yuexunit.finance.member.action.v1.model.FinanceSettlementStatementDetail;
import com.yuexunit.finance.member.action.v1.model.MonthEarningDetail;
import com.yuexunit.finance.member.action.v1.model.ReceiptRecordDetail;
import com.yuexunit.finance.member.entity.MonthEarningDetailEntity;
import com.yuexunit.finance.member.entity.MonthEarningEntity;
import com.yuexunit.finance.member.service.MonthEarningDetailService;
import com.yuexunit.finance.member.service.MonthEarningService;
import com.yuexunit.finance.project.action.v1.dto.FinanceLoanCastor;
import com.yuexunit.finance.project.entity.FinanceMoneyStatementEntity;
import com.yuexunit.finance.project.entity.FinanceSettlementStatementEntity;
import com.yuexunit.finance.project.entity.ReceiptRecordEntity;
import com.yuexunit.finance.project.service.FinanceMoneyStatementService;
import com.yuexunit.finance.project.service.FinanceSettlementStatementService;
import com.yuexunit.finance.project.service.ReceiptRecordService;
import com.yuexunit.finance.thread.EnterpriseSummaryTimeTask;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.annotation.Transactional;

@Implements(MonthEarningApi.class)
public class MonthEarningApiImpl implements MonthEarningApi {
	
	private MonthEarningService monthEarningService = ComponentFactory.loadClass(MonthEarningService.class); 

	private FinanceSettlementStatementService financeSettlementStatementService = ComponentFactory.loadClass(FinanceSettlementStatementService.class);
	
	private MonthEarningDetailService monthEarningDetailService = ComponentFactory.loadClass(MonthEarningDetailService.class);
	
	private FinanceMoneyStatementService financeMoneyStatementService = ComponentFactory.loadClass(FinanceMoneyStatementService.class);
	
	private ReceiptRecordService receiptRecordService = ComponentFactory.loadClass(ReceiptRecordService.class);
	
	@Override
	public PageList<MonthEarningDetail> inquiryMonthEarningPageList(
			Integer year, Integer month,String monthEarningNumber,
			String enterpriseName, Date earningDateStar, Date earningDateEnd, Integer confirmStatus, PageCriteria page) {
		PageList<MonthEarningEntity> monthEarningEntityList = monthEarningService.inquiryMonthEarningPageList(year, month, monthEarningNumber, enterpriseName, earningDateStar, earningDateEnd, confirmStatus, page);
		PageList<MonthEarningDetail> monthEarningDetailList = new PageList<>(page, monthEarningEntityList.getTotalRecordCount());
		for (MonthEarningEntity monthEarningEntity : monthEarningEntityList) {
			MonthEarningDetail monthEarningDetail = new MonthEarningDetail();
			BeanUtils.copyProperties(monthEarningEntity, monthEarningDetail);
			
			monthEarningDetailList.add(monthEarningDetail);
		}
		return monthEarningDetailList;
	}
	
	@Override
	public List<MonthEarningDetail> inquiryMonthEarningList(Integer year, Integer month, String monthEarningNumber,
			String enterpriseName, Date earningDateStar, Date earningDateEnd) {
		List<MonthEarningEntity> monthEarningEntityList = monthEarningService.inquiryMonthEarningList(year, month, monthEarningNumber, enterpriseName, earningDateStar, earningDateEnd);
		List<MonthEarningDetail> monthEarningDetailList = new ArrayList<MonthEarningDetail>();
		for (MonthEarningEntity monthEarningEntity : monthEarningEntityList) {
			MonthEarningDetail monthEarningDetail = new MonthEarningDetail();
			BeanUtils.copyProperties(monthEarningEntity, monthEarningDetail);
			
			monthEarningDetailList.add(monthEarningDetail);
		}
		return monthEarningDetailList;
	}

	@Override
	public MonthEarningDetail inquiryMonthEarningDetail(
			String monthEarningNumber, Long enterpriseId, Integer year, Integer month) {
		List<MonthEarningEntity> monthEarningDetailList = monthEarningService.inquiryMonthEarningDetail(monthEarningNumber, enterpriseId, year, month);
		if(monthEarningDetailList == null || monthEarningDetailList.size() == 0) {
			return null;
		}
		MonthEarningEntity monthEarningEntity = monthEarningDetailList.get(0);
		MonthEarningDetail monthEarningDetail = new MonthEarningDetail();
		
		BeanUtils.copyProperties(monthEarningEntity, monthEarningDetail);
		
		List<FinanceSettlementStatementDetail> ffsds = new ArrayList<>();
		List<FinanceSettlementStatementEntity> fsses = financeSettlementStatementService.inquireFinanceSettlementStatementList(monthEarningEntity.getMonthEarningId());
		for (FinanceSettlementStatementEntity fsse : fsses) {
			FinanceSettlementStatementDetail ffsd = new FinanceSettlementStatementDetail();
			BeanUtils.copyProperties(fsse, ffsd);
			
			List<ReceiptRecordDetail> rrds = new ArrayList<>();
			List<ReceiptRecordEntity> rres = receiptRecordService.inquireReceiptRecordList(fsse.getSettlementStatementId());
			for (ReceiptRecordEntity rre : rres) {
				ReceiptRecordDetail rrd = new ReceiptRecordDetail();
				BeanUtils.copyProperties(rre, rrd);
				rrds.add(rrd);
			}
			
			ffsd.setReceiptRecordList(rrds);
			
			ffsds.add(ffsd);
		}
		monthEarningDetail.setFinanceSettlementStatementList(ffsds);
		
		return monthEarningDetail;
	}

	@Override
	@Transactional
	public void confirmMonthEarningDetail(Long monthEarningId) {
		MonthEarningEntity monthEarningEntity = monthEarningService.inquiryMonthEarningDetail(monthEarningId);
		if (monthEarningEntity.getConfirmStatus() == 1) {
			throw new BusinessException("不能重复确认！");
		}
		monthEarningEntity.setConfirmPerson(FinanceSessionContext.getAccountFullName());
		monthEarningEntity.setConfirmDate(DateUtils.now());
		monthEarningEntity.setConfirmStatus(1);
		monthEarningService.updateMonthEarning(monthEarningEntity);
		
		FinanceSettlementStatementEntity fsse = new FinanceSettlementStatementEntity();
		fsse.setCreator(FinanceSessionContext.getAccountFullName());
		fsse.setCreatorId(FinanceSessionContext.getAccountId());
		fsse.setSettlementNumber(monthEarningEntity.getMonthEarningNumber());
		fsse.setSettlementDateTime(monthEarningEntity.getEarningDate());
		fsse.setEnterpriseId(monthEarningEntity.getEnterpriseId());
		fsse.setEnterpriseName(monthEarningEntity.getEnterpriseName());
		fsse.setAmount(monthEarningEntity.getRemainingMoney());
		fsse.setBalance(monthEarningEntity.getRemainingMoney());
		fsse.setMonthEarningId(monthEarningId);
		financeSettlementStatementService.createFinanceSettlementStatement(fsse);
		
		List<MonthEarningDetailEntity> medes = monthEarningDetailService.inquiryMonthEarningDetailList(monthEarningEntity.getYear(), monthEarningEntity.getMonth(), null, null, monthEarningEntity.getEnterpriseId());
		for (MonthEarningDetailEntity mede : medes) {
			FinanceMoneyStatementEntity statement = FinanceLoanCastor.fromMonthEarningDetail(mede);
			financeMoneyStatementService.createFinanceMoneyStatement(statement);
		}
	}

	@Override
	public void reCountMonthInterest(Integer year, Integer month, String enterpriseName, Long enterpriseId) {
		EnterpriseSummaryTimeTask task = new EnterpriseSummaryTimeTask();
		task.autoCountMonthInterest(year, month, enterpriseName, enterpriseId);
	}
}
